package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.Articles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class
ArticleController {
    //创建目录存储作者信息
    private static File articlesDir;

    static {
        articlesDir = new File("./articles");
        if (!articlesDir.exists()) {
            articlesDir.mkdirs();
        }
    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理文章发表");

        //获取表单信息
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);

        //必要的验证
        if (title == null || title.isEmpty() ||
                author == null || author.isEmpty() ||
                content == null || content.isEmpty()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        //保存文章数据
        Articles articles = new Articles(title, author, content);
        File file = new File(articlesDir, title + ".obj");

        if (file.exists()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(articles);
            response.sendRedirect("/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理显示文章列表");

        //1读取articles目录下的所有obj文件,并将读取出来的所有Article对象存入集合备用
        List<Articles> articlesList = new ArrayList<>();

        File[] subs = articlesDir.listFiles(f -> f.getName().endsWith(".obj"));

        for (File sub : subs) {
            try (FileInputStream fis = new FileInputStream(sub);
                 ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Articles articles = (Articles) ois.readObject();
                articlesList.add(articles);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        try {
            //2通过response获取输出流PrintWriter
            response.setContentType("text/html;charset=utf-8");

            //3通过输出流发送HTML代码并包含所有文章信息
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");

            pw.println("<table border='1'>");

            pw.println("<tr>");
            pw.println("<td>文章标题</td>");
            pw.println("<td>文章作者</td>");
            pw.println("</tr>");

            for (Articles articles : articlesList) {
                pw.println("<tr>");
                pw.println("<td>" + articles.getTitle() + "</td>");
                pw.println("<td>" + articles.getAuthor() + "</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");

            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
